//
//  HSYCustomPhotoModel.m
//  高仿暴走斗图
//
//  Created by key on 16/6/6.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYCustomPhotoModel.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface HSYCustomPhotoModel ()

@end

@implementation HSYCustomPhotoModel

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self setupWithALAssetType:kALAssetTypePhoto];
    }
    return self;
}

- (instancetype)initWithALAssetType:(kALAssetType)type {
    
    if (self = [super init]) {
        
        [self setupWithALAssetType:type];
    }
    return self;
}

- (void)setupWithALAssetType:(kALAssetType)type {
    
    _photos = [[NSMutableArray alloc] initWithCapacity:10];
    _fullScreenImages = [NSMutableArray arrayWithCapacity:10];
    _assetType = type;
}

- (void)reloadDataForIsAddObject:(BOOL)addObject withIndex:(NSInteger)index {
    
    NSDictionary *dic = self.photos[index];
    ALAsset *asset = [dic.allValues firstObject];
    UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
    if (addObject) {
        
        [self.fullScreenImages addObject:image];
        [self.photos replaceObjectAtIndex:index withObject:@{
                                                             @(YES) : asset,
                                                             }];
    } else {
        [self.photos replaceObjectAtIndex:index withObject:@{
                                                             @(NO) : asset,
                                                             }];
        [self.fullScreenImages removeAllObjects];
        for (NSDictionary *dic in self.photos) {
            if ([dic.allKeys.firstObject boolValue]) {
                [self.fullScreenImages addObject:[UIImage imageWithCGImage:[[[dic.allValues firstObject] defaultRepresentation] fullScreenImage]]];
            }
        }
    }
}

- (void)photosAddObject:(NSDictionary *)objcet {
    
    [self.photos addObject:objcet];
}

- (void)photosRemoveAllObjects {
    
    [self.photos removeAllObjects];
}

@end
